from app import db
from datetime import datetime

class SysConfig(db.Model):
    __tablename__ = 'sys_config'

    config_id = db.Column(db.Integer, primary_key=True, info='参数主键')
    config_name = db.Column(db.String(100, 'utf8mb4_0900_ai_ci'), info='参数名称')
    config_key = db.Column(db.String(100, 'utf8mb4_0900_ai_ci'), info='参数键名')
    config_value = db.Column(db.String(500, 'utf8mb4_0900_ai_ci'), info='参数键值')
    config_type = db.Column(db.String(1, 'utf8mb4_0900_ai_ci'), server_default=db.FetchedValue(), info='系统内置（Y是 N否）')
    create_by = db.Column(db.String(64, 'utf8mb4_0900_ai_ci'), info='创建者')
    create_time = db.Column(db.DateTime, server_default=db.FetchedValue(), info='创建时间')
    update_by = db.Column(db.String(64, 'utf8mb4_0900_ai_ci'), info='更新者')
    update_time = db.Column(db.DateTime, info='更新时间')
    remark = db.Column(db.String(500, 'utf8mb4_0900_ai_ci'), info='备注')

    def to_json(self):
        if self.create_time and isinstance(self.create_time, datetime):
            self.create_time = self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        if self.update_time and isinstance(self.update_time, datetime):
            self.update_time = self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        return {
            'configId': self.config_id,
            'configName': self.config_name,
            'configKey': self.config_key,
            'configValue': self.config_value,
            'configType': self.config_type,
            'createBy': self.create_by,
            'createTime': self.create_time,
            'updateBy': self.update_by,
            'updateTime': self.update_time,
            'remark': self.remark,
        }